import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Employee} from "./employee.type"

//定义url常量，指向本地服务器的地址
const url = 'http://localhost:8080'

//标记该类为可注入的依赖
@Injectable({
  providedIn: 'root', //将该服务注册到根模块，指定全局可用
})

/**
 * 员工服务类
 */
export class EmployeeService {

  //构造函数，私有化http客户端
  constructor(private http: HttpClient) {
  }

  //分页条件查询
  page(pageNum: number, pageSize: number, name: string, gender: string) {
    return this.http.get<Employee[]>(`${url}/employee?pageNum=${pageNum}&pageSize=${pageSize}&name=${name}&gender=${gender}`, {
      observe: 'response' //观察响应，以便能够处理完整的HTTP响应
    });
  }

  //删除
  remove(id: number) {
    return this.http.delete(`${url}/employee/remove/${id}`)
  }

  //添加
  add(employee: Employee) {
    return this.http.post(`${url}/employee/add`, employee)
  }

  //根据ID查询
  getById(id: number) {
    return this.http.get<Employee>(`${url}/employee/findById/${id}`)
  }

  //编辑
  edit(id: number, employee: Employee) {
    return this.http.put<Employee>(`${url}/employee/edit/${id}`, employee)
  }
}